package com.jstyle.jclife.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Space;

import com.jstyle.countrylibrary.CountryPage;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.SosContact;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.NetWorkConast;
import com.jstyle.jclife.utils.SchedulersTransformer;
import com.jstyle.jclife.view.EditTextWithDelete;
import com.mob.tools.FakeActivity;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.SMSSDK;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class AddContactActivity extends BaseActivity {

    public final static String CONTACT = "CONTACT";
    @BindView(R.id.bt_feed_back)
    ImageView btFeedBack;
    @BindView(R.id.bt_title)
    Button btTitle;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.account_space)
    Space accountSpace;
    @BindView(R.id.bt_login_getCountryCode)
    Button btLoginGetCountryCode;
    @BindView(R.id.et_phonenumber)
    EditTextWithDelete etPhonenumber;
    @BindView(R.id.VIEW)
    View VIEW;
    @BindView(R.id.et_remake)
    EditTextWithDelete etRemake;
    @BindView(R.id.rl_input)
    RelativeLayout rlInput;
    @BindView(R.id.bt_delete)
    Button btDelete;
    @BindView(R.id.bt_edit)
    Button btEdit;
    private SosContact.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        dataBean = (SosContact.DataBean) getIntent().getExtras().get(CONTACT);
        if (dataBean == null) {
            btDelete.setEnabled(false);
        }else{
            phone=dataBean.getPhone().split("-")[1];
            countryCode=dataBean.getPhone().split("-")[0]+"-";
            btLoginGetCountryCode.setText("+"+dataBean.getPhone().split("-")[0].substring(2));
            etPhonenumber.setText(phone);
            remake= (String) dataBean.getRemake();
            if(!TextUtils.isEmpty(remake))etRemake.setText(remake);
        }
    }

    @OnClick({R.id.bt_feed_back, R.id.bt_login_getCountryCode, R.id.bt_delete, R.id.bt_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_feed_back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.bt_login_getCountryCode:
                getCountryCode();
                break;
            case R.id.bt_delete:
                deleteContact();
                break;
            case R.id.bt_edit:
                addOrUpdate();
                break;
        }
    }

    private void addOrUpdate() {
        if(dataBean==null){
            addContact();
        }else{
            updateContact();
        }
    }
    String phone="";
    String remake="";
    private void updateContact() {
        String number=etPhonenumber.getText().toString();
         remake=etRemake.getText().toString();
        if (TextUtils.isEmpty(number)) {
            return;
        }
        phone=countryCode+number;
        String uid= NetWorkUtil.getUserId();
        UserInfo userInfo= UserInfoDaoManager.getUserByUid(uid);
        String uName=userInfo.getName();
        String id= String.valueOf(dataBean.getId());
        showProgressDialog("Waiting...");
        NetWorkUtil.getInstance().getJstyleApi().editContact(id,uid,uName,phone,remake).compose(SchedulersTransformer.<SosContact>io2MainObservable()).subscribe(new Observer<SosContact>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SosContact sosContact) {
                disMissProgressDialog();
                if(sosContact.getMsgCode()== NetWorkConast.SUCCESS){
                    showToast(getString(R.string.Successful));
                    setResult(RESULT_OK);
                    finish();
                }else{
                    showToast(sosContact.getMsgInfo());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void addContact() {
        String number=etPhonenumber.getText().toString();
        if (TextUtils.isEmpty(number)) {
            return;
        }
        phone=countryCode+number;
        remake=etRemake.getText().toString();
        String uid=NetWorkUtil.getUserId();
        UserInfo userInfo= UserInfoDaoManager.getUserByUid(uid);
        String uName=userInfo.getName();
        if(TextUtils.isEmpty(uName))uName="ARM";
        showProgressDialog("Waiting...");
        NetWorkUtil.getInstance().getJstyleApi().addContact(uid,uName,phone,remake).compose(SchedulersTransformer.<SosContact>io2MainObservable()).subscribe(new Observer<SosContact>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SosContact sosContact) {
                disMissProgressDialog();
                if(sosContact.getMsgCode()== NetWorkConast.SUCCESS){
                    showToast(getString(R.string.Successful));
                    setResult(RESULT_OK);
                    finish();
                }else{
                    showToast(sosContact.getMsgInfo());
                }
            }

            @Override
            public void onError(Throwable e) {
                    disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void deleteContact() {
        int id=dataBean.getId();
        showProgressDialog("Waiting...");
        NetWorkUtil.getInstance().getJstyleApi().deleteContact(String.valueOf(id)).compose(SchedulersTransformer.<SosContact>io2MainObservable()).subscribe(new Observer<SosContact>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SosContact sosContact) {
                disMissProgressDialog();
                if(sosContact.getMsgCode()== NetWorkConast.SUCCESS){
                    showToast(getString(R.string.Successful));
                    setResult(RESULT_OK);
                    finish();
                }else{
                    showToast(sosContact.getMsgInfo());
                }

            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    String countryCode="001-";
    private void getCountryCode() {
        CountryPage phone = new CountryPage();
        phone.setCountryId("1");
        phone.showForResult(this, (Intent) null, new FakeActivity() {
            @Override
            public void onResult(HashMap<String, Object> data) {
                super.onResult(data);
                String currentId = (String) data.get("id");
                String[] res = SMSSDK.getCountry(currentId);
                btLoginGetCountryCode.setText("+" + res[1]);
                countryCode = "00" + res[1] + "-";
            }
        });
    }
}
